<template>
  <div class="app-container">
    <content-title></content-title>
    <el-form ref="formData" :model="formData" :rules="rules" class="form-container"
             label-width="100px" label-position="right">
      <el-form-item label="角色名称" prop="name">
        <el-input v-model="formData.name"></el-input>
      </el-form-item>
      <el-form-item label="描述" prop="description">
        <el-input v-model="formData.description"></el-input>
      </el-form-item>
      <privilege-grant ref="privilegeTree" :role-id="roleId"></privilege-grant>

      <el-form-item>
        <go-back :opt-type="optType"></go-back>
        <el-button type="primary" @click="onSubmit" v-show="optType!=3">保存</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>

  import {detail, save, update} from "@/api/role";
  import privilegeGrant from '@/views/components/privilege-grant'

  export default {
    name: 'roleDetail',
    components: {privilegeGrant},
    props: {
      optType: {
        type: Number,
        default: 3
      }
    },
    data() {
      return {
        allResources: '',
        formData: {
          id: '',
          name: '',
          resourceIds: '',
          description: '',
        },
        rules: {
          name: [
            {required: true, message: '请输入角色名称', trigger: 'blur'},
          ],
        },
        tempRoute: {}
      }
    },
    computed: {
      roleId(){
        return this.formData.id
      }
    },
    created() {
      this.initOthers()
      this.getDetail()
      this.tempRoute = Object.assign({}, this.$route)
    },
    methods: {
      initOthers() {
      },
      getDetail() {
        this.$business.getDetailById(this, detail, (data) => {
          console.log("角色获取详情返回有问题L",data)
          this.formData = data
        })
      },
      onSubmit() {
        this.formData.resourceIds = this.$refs.privilegeTree.getAllCheckedKeys().join(",");
        this.$business.save(this, save, update, "roleList");
      }
    }
  }
</script>

